package com.novellotus.iov.carvice.model;

import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;

import com.novellotus.iov.carvice.activity.AutographActivity;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Rich on 2017/8/29.
 */

public class AutographModel {
    private AutographActivity activity;
    public boolean hasSign;

    public AutographModel(AutographActivity activity) {
        this.activity = activity;
    }

    /**
     * 确认签名
     */
    public void confirmSign(Gesture gesture) {
        if (hasSign) {
            Bitmap bitmap = gesture.toBitmap(640, 360, 12, Color.BLACK);
            String path = Environment.getExternalStorageDirectory().toString();
            try {
                File file = new File(path + "/sign.png");
//                if (file.exists()) {
//                    file = new File(path + "/sign.png");
//                }
                FileOutputStream fos;

                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent();
            intent.putExtra("path", path + "/sign.png");
            activity.setResult(100, intent);
            activity.finish();
        } else {
            MyUtils.showToast(activity, "请签名再点击确定");
        }
    }


    /**
     * 清空画板或者返回上级页面
     */
    public void clear(GestureOverlayView gestureView) {
        gestureView.setFadeOffset(10);//清除前设置时间间隔缩小
        gestureView.clear(true);
        gestureView.setFadeOffset(3600000);//清除后恢复时间间隔
        hasSign = false;
    }
}
